package game.resource
{
	import flash.events.Event;
	
	import org.bulkLoader.comply.BulkFile;
	import org.bulkLoader.comply.BulkLoaderDefine;
	import org.bulkLoader.events.BulkEvent;
	import org.bulkLoader.events.BulkItemLoadingEvent;
	import org.bulkLoader.handler.BulkHandler;
	import org.bulkLoader.loadinginfo.BulkItemLoadingInfo;

	public class ResourceLoadHelper
	{
		public function ResourceLoadHelper()
		{
		}
		
		private static var _instance:ResourceLoadHelper = new ResourceLoadHelper();
		
		public static function loadMapGrid(res:int, loadCompleteFunc:Function):void
		{
			var resID:String = ResourceIDHelper.getMapGridID(res);
			var resURL:String = ResourceUrlHelper.getMapGridUrl(res);
			
			_instance.load(resID, resURL, loadCompleteFunc, null);
		}
		
		////////////////////////////////////////////////////////////////////////////////
		private function load(
			resID:String, 
			resURL:String, 
			loadCompleteFunc:Function, 
			loadErrorFunc:Function, 
			type:String = BulkLoaderDefine.FILE_AUTOMATIC):void
		{
			var paramter:Object = {};
			paramter.resID = resID;
			paramter.loadCompleteFunc = loadCompleteFunc;
			paramter.loadErrorFunc = loadErrorFunc;
			var file:BulkFile = new BulkFile(resURL, paramter);
			
			var loadHandlerCls:Class = BulkLoaderDefine.getHandlerCLass(file.type);
			if (!loadHandlerCls)
				return;
			
			var loadHandler:BulkHandler = new loadHandlerCls(file, new BulkItemLoadingInfo());
			loadHandler.addEventListener(BulkItemLoadingEvent.ITEM_COMPLETED, onLoadComplete);
			loadHandler.addEventListener(BulkItemLoadingEvent.ITEM_ERROR, onLoadError);
			loadHandler.addEventListener(BulkItemLoadingEvent.ITEM_START, onLoadStart);
			loadHandler.addEventListener(BulkItemLoadingEvent.ITEM_PROGRESS, onLoadProgress);
			loadHandler.load();
		}
		
		protected function onLoadProgress(event:BulkItemLoadingEvent):void
		{
			
		}
		
		protected function onLoadStart(event:BulkItemLoadingEvent):void
		{
			
		}
		
		protected function onLoadError(event:BulkItemLoadingEvent):void
		{
			
		}
		
		protected function onLoadComplete(event:BulkItemLoadingEvent):void
		{
			var paramter:Object = event.file.ownParameter;
			if (paramter.loadCompleteFunc)
				paramter.loadCompleteFunc(event.loadingInfo.getData());
		}
	}
}